﻿using SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XMFramework.Models;
using XMFramework.MysqlService;

namespace XMFramework.Forms.Pages.Products
{
    public partial class FProduct : UIPage
    {
        private SqlSugarScope db = SqlSugarHelper.Db;
        List<ProductionOrderModel> orderlist = new List<ProductionOrderModel>();
        public FProduct()
        {
            InitializeComponent();
        }

        private void FProduct_Initialize(object sender, EventArgs e)
        {
            if (!backgroundWorker1.IsBusy)
                backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                db.BeginTran();
                orderlist = db.Queryable<ProductionOrderModel>().Where(it => it.state != "新增").OrderBy(it => it.deviceCode, OrderByType.Asc).ToList();
                db.CommitTran();
            }
            catch (Exception ex)
            {
                db.RollbackTran();//数据回滚
                UIMessageTip.ShowError("插入失败 - 数据回滚!\n" + ex);
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            uiDataGridView1.DataSource = orderlist;
            uiSymbolButton1.Enabled = false;
        }

        private void uiDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                uiTextBox1.Text = uiDataGridView1.Rows[e.RowIndex].Cells["id"].Value.ToString();//工艺编号

                if (uiDataGridView1.Rows[e.RowIndex].Cells["state"].Value.ToString() == "准备")
                {
                    uiSymbolButton1.Enabled = true;
                }
                UIMessageTip.ShowOk("查看 - 状态");
            }
        }

        private void uiSymbolButton3_Click(object sender, EventArgs e)
        {
            //搜索按钮
            try
            {
                db.BeginTran();

                uiDataGridView1.DataSource = db.Queryable<ProductionOrderModel>().
                    Where(it => it.state != "新增" && it.orderCode.Contains(uiTextBox5.Text) && it.recipeCode.Contains(uiTextBox11.Text) && it.deviceCode.Contains(uiTextBox7.Text)).
                    OrderBy(it => it.deviceCode, OrderByType.Asc).ToList();

                db.CommitTran();
            }
            catch (Exception ex)
            {
                db.RollbackTran();//数据回滚
                UIMessageTip.ShowError("插入失败 - 数据回滚!\n" + ex);
            }
            uiSymbolButton1.Enabled = false;
        }

        private void uiSymbolButton4_Click(object sender, EventArgs e)
        {
            //清除按钮
            uiTextBox5.Clear();//工艺编号
            uiTextBox11.Clear();//原料密度
            uiTextBox7.Clear();//模具编号

            if (!backgroundWorker1.IsBusy)
                backgroundWorker1.RunWorkerAsync();
        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            //驳回生产任务按钮
            try
            {
                db.BeginTran();

                int count = db.Updateable<ProductionOrderModel>()
                    .SetColumns(it => new ProductionOrderModel()
                    {
                        state = "新增"
                    })
                    .Where(it => it.id == Convert.ToInt32(uiTextBox1.Text)).ExecuteCommand();

                db.CommitTran();

                if (count > 0)
                    UIMessageTip.ShowOk("下达任务 - 成功");
                else
                    UIMessageTip.ShowWarning("未查询到生产任务 - 警告");

            }
            catch (Exception ex)
            {
                db.RollbackTran();//数据回滚
                UIMessageTip.ShowError("下达失败 - 数据回滚!" + ex);
            }

            if (!backgroundWorker1.IsBusy)
                backgroundWorker1.RunWorkerAsync();




        }

    }
}
